package com.example.home

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.viewModels
import androidx.lifecycle.viewModelScope
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.example.common.view.adapter.ImageBannerAdapter
import com.example.common.view.view.BaseMvvmFragment
import com.example.db.entity.BannerEntity
import com.example.home.adapter.HomeAdapter
import com.example.home.adapter.HomeUIAdapter
import com.example.home.databinding.FragmentHomeBinding
import com.example.home.entity.HomeUIEntity
import com.example.home.vm.HomeViewModel
import com.youth.banner.indicator.CircleIndicator
import com.youth.banner.listener.OnPageChangeListener
import com.youth.banner.transformer.AlphaPageTransformer
import dagger.hilt.android.AndroidEntryPoint


@AndroidEntryPoint
class HomeFragment : BaseMvvmFragment<FragmentHomeBinding>() {
    override fun getLayoutId(): Int =R.layout.fragment_home
    val homeAdapter=HomeAdapter()
    val homeUIAdapter=HomeUIAdapter()
    private val homeViewModel:HomeViewModel by viewModels()
    val homeList: MutableList<HomeUIEntity> = mutableListOf()
    private val bannerList= mutableListOf<BannerEntity>()
    private lateinit var bannerAdapter: ImageBannerAdapter
    override fun initView() {
        //轮播图
        bannerList.add(BannerEntity(0,R.drawable.pmou1,""))
        bannerList.add(BannerEntity(1,R.drawable.h1,""))
        bannerList.add(BannerEntity(2,R.drawable.h2,""))
        bannerList.add(BannerEntity(3,R.drawable.h3,""))
        bannerAdapter=ImageBannerAdapter(bannerList)
        mDataBinding.banner.setAdapter(bannerAdapter)
        mDataBinding.banner.addBannerLifecycleObserver(this)
        mDataBinding.banner.addPageTransformer(AlphaPageTransformer())

        //添加UI数桔
        homeList.clear()
        homeList.add(HomeUIEntity(0,R.drawable.park_home,"车位管理"))
        homeList.add(HomeUIEntity(1,R.drawable.visitor_home,"访客登记"))
        homeList.add(HomeUIEntity(2,R.drawable.repair_home,"物业报修"))
        homeList.add(HomeUIEntity(3,R.drawable.sign_home,"考勤打卡"))
        homeList.add(HomeUIEntity(4,R.drawable.house_home,"房源信息"))
        homeList.add(HomeUIEntity(5,R.drawable.shop_home,"商家推荐"))
        homeList.add(HomeUIEntity(6,R.drawable.member_home,"月卡会员"))
        homeList.add(HomeUIEntity(7,R.drawable.device_home,"设备巡检"))
        //园区公告
        mDataBinding.rv.layoutManager=LinearLayoutManager(requireContext())
        mDataBinding.rv.adapter=homeAdapter
        homeViewModel.getListData(0,10)
        homeViewModel.mSuccessLiveData.observe(this){
            homeAdapter.data.clear()
            homeAdapter.data.addAll(it.data)
            homeAdapter.notifyDataSetChanged()
        }
        homeViewModel.mErrorLiveData.observe(viewLifecycleOwner){
            ToastUtils.showLong(it)
        }
        //UI数据
        mDataBinding.rvUi.adapter=homeUIAdapter
        mDataBinding.rvUi.layoutManager=GridLayoutManager(requireContext(),4)
        homeUIAdapter.data.clear()
        homeUIAdapter.data.addAll(homeList)
        homeUIAdapter.notifyDataSetChanged()
        homeUIAdapter.setOnItemClickListener { adapter, view, position ->
            when(homeUIAdapter.data[position].name){
                "设备巡检"->{
                    ARouter.getInstance().build("/device/DeviceOrderActivity").navigation()
                }
                "房源信息"->{
                    ARouter.getInstance().build("/house/HouseActivity").navigation()
                }
                "月卡会员"->{
                    ARouter.getInstance().build("/member/MemberActivity").navigation()
                }
            }
        }

    }

}